using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
namespace DAL
{
    public class IPRelationships : DAL.DataManager<IPRelationship>
    {
        private static IPRelationships Instance = new IPRelationships();
        static IPRelationships() { }

        public override string TableName
        {
            get { return @"fstdt_iprelationships"; }
        }

        protected override void CopyToItem(DataRow Row, IPRelationship Item)
        {
            Item.DateCreated = Convert.ToDateTime(Row["DateCreated"]);
            Item.Banned = Convert.ToBoolean(Row["Banned"]);

            if (Row["UserID"] != System.DBNull.Value)
            {
                Item.User = Users.Item(Convert.ToInt64(Row["UserID"]));
            }
            else
            {
                Item.User = null;
            }
        }

        protected override void CopyFromItem(DataManagerQuery query, IPRelationship Item)
        {
            query.AddParam("DateCreated", Item.DateCreated);
            query.AddParam("Banned", Item.Banned);
            if (Item.User != null)
            {
                query.AddParam("UserID", Item.User.ID);
            }
            else
            {
                query.AddParam("UserID", null);
            }
        }

        public static IPRelationship Item(Int64 id)
        {
            return Instance.GetItem(id);
        }

        public static List<IPRelationship> All()
        {
            return Instance.GetItems();
        }

        public static void Add(IPRelationship Item)
        {
            Item.DateCreated = DateTime.Now;
            Item.Banned = false;
            Item.User = Managers.UserLoginManager.CurrentUser;
            Instance.Insert(Item);
        }

        public static void Save(IPRelationship Item)
        {
            Instance.Update(Item);
        }

        public static void BanIP(IPRelationship Item)
        {
            Item.Banned = true;
            Save(Item);
        }

        public static void UnbanIP(IPRelationship Item)
        {
            Item.Banned = false;
            Save(Item);
        }

        public static void AnnihilateByIPRelationship(IPRelationship Item)
        {
            Item.Banned = true;
            Save(Item);
            Comments.UpdateByIPRelationship(Item);
        }

        public static void UnannihilateByIPRelationship(IPRelationship Item)
        {
            Item.Banned = false;
            Save(Item);
            Comments.UpdateByIPRelationship(Item);
        }

        public static IPRelationship GetByUser(User user)
        {
            if (user != null)
            {
                DataManagerQuery query = new DataManagerQuery();
                query.AddParam("UserID", user.ID);
                List<IPRelationship> items = Instance.GetItems(query);
                if (items.Count > 0)
                {
                    return items[0];
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public static void UpdateTrackingByRelationshipAndUser(ref IPRelationship relationship, User user)
        {
            IPRelationship previousUserRelationship = GetByUser(user);
            if (previousUserRelationship != null && previousUserRelationship != relationship)
            {
                // all comments that are pointing to currentRelationship need to point to the
                // previousUserRelationship in order to keep comment tracking by user consistent.

                // modifying comments to point to previous user relationship
                foreach (Comment comment in Comments.GetByIPRelationship(relationship))
                {
                    comment.IPRelationship = previousUserRelationship;
                    Comments.Save(comment);
                }

                Instance.Delete(relationship); // removing obsolete relationship

                relationship = previousUserRelationship;
            }
        }
    }

    public class IPRelationship : DAL.DataManagerRow
    {
        private DateTime _DateCreated;
        public DateTime DateCreated
        {
            get
            {
                return _DateCreated;
            }
            set
            {
                _DateCreated = value;
            }
        }

        private bool _banned;
        public bool Banned
        {
            get
            {
                return _banned;
            }
            set
            {
                _banned = value;
            }
        }

        private User _user;
        public User User
        {
            get
            {
                return _user;
            }
            set
            {
                _user = value;
            }
        }
    }
}
